package com.atguigu.gulimall.order.intercept;

import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.vo.MemberResp;
import com.atguigu.gulimall.order.utils.LoginUserUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.util.Enumeration;
import java.util.Objects;

/**
 * @Description
 * @Author lhj
 * @Date 2024/5/1 16:41
 */
@Slf4j
public class OrderInterceptor implements HandlerInterceptor {
    static Logger logger = LoggerFactory.getLogger(OrderInterceptor.class);
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        AntPathMatcher pathMatcher = new AntPathMatcher();
        boolean match = pathMatcher.match("/order/order/**", uri);
        boolean payedPass = pathMatcher.match("/payed/notify", uri);
        if (match || payedPass) {
            HttpSession session = request.getSession();
            //获取Redis缓存信息
            Object loginInfo = session.getAttribute(AuthServerConstant.LOGIN_USER);
            if (loginInfo != null && LoginUserUtils.get() == null) {
                MemberResp memberResp = (MemberResp) loginInfo;
                LoginUserUtils.set(memberResp);
            }

            log.info("PreHandle passed for URI: {}", uri);

            // 打印请求头
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                String headerValue = request.getHeader(headerName);
                log.info("Header: {} = {}", headerName, headerValue);
            }

            // 打印请求体
            StringBuilder sb = new StringBuilder();
            BufferedReader reader = request.getReader();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            log.info("Request Body: {}", sb.toString());
            
            return true;
        }
        HttpSession session = request.getSession();
        //获取Redis缓存信息
        Object loginInfo = session.getAttribute(AuthServerConstant.LOGIN_USER);
        //没有登陆重定向到登录页面
        if (ObjectUtils.isEmpty(loginInfo)) {
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
        //登陆成功后将用户信息保存到ThreadLocal 以供当前线程在其他任何地方获取使用
        MemberResp memberResp = (MemberResp) loginInfo;
        LoginUserUtils.set(memberResp);
        
        return true;
    }
}
